package com.jiangnan.warehouse_management.controller;


import com.jiangnan.warehouse_management.common.Result;
import com.jiangnan.warehouse_management.dto.request.CategoryRequest;
import com.jiangnan.warehouse_management.dto.request.CategoryUpdateRequest;
import com.jiangnan.warehouse_management.dto.response.CategoryQueryResponse;
import com.jiangnan.warehouse_management.pojo.Category;
import com.jiangnan.warehouse_management.service.CategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
@Validated
public class CategoryController {
    @Autowired
    private final CategoryService categoryService;

    @GetMapping("/getCategories")
    Result<CategoryQueryResponse> getCategories(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                @RequestParam(value = "categoryName", required = false) String categoryName) {
        CategoryQueryResponse data = categoryService.getCategories(page, pageSize, categoryName);
        return Result.success(data);
    }
    @PostMapping("/createCategory")
    Result<Category> createCategory(@RequestBody CategoryRequest request) {
        Category data = new Category();
        data = categoryService.createCategory(request);
        return Result.success(data);
    }

    @GetMapping("categoriesById/{id}")
    Result<CategoryQueryResponse.CategoryResponse> getCategoryById(@PathVariable("id") Integer id) {
        CategoryQueryResponse.CategoryResponse data = categoryService.getCategoryById(id);
        return Result.success(data);
    }

    @PutMapping("/updateCategory/{id}")
    public Result<Category> updateCategory(@PathVariable Integer id,
                                            @RequestBody CategoryUpdateRequest request) {
        Category updatedCategory = categoryService.updateCategory(id, request);
        return Result.success(updatedCategory);
    }

    @DeleteMapping("/deleteCategory/{id}")
    public Result<Void> deleteCategory(@PathVariable Integer id) {
        categoryService.deleteCategory(id);
        return Result.success();
    }
}
